<?php
// +----------------------------------------------------------------------
// | CLTPHP.Pro [权限管理控制器]
// +----------------------------------------------------------------------
// | Copyright (c) 2019-2029 https://www.cltphp.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( https://www.cltphp.com/detail-140.html )
// +----------------------------------------------------------------------
// | Author: chichu <1109305987@qq.com>
// +----------------------------------------------------------------------

namespace app\admin\controller;

use app\admin\model\AuthGroup;
use app\admin\model\AuthGroupAccess;
use app\admin\model\AuthRule;

use think\facade\Request;

class Auth extends Base
{
    /********************************用户组管理*******************************/
    //用户组管理
    public function group()
    {
        if(request()->isPost()){
            $list = AuthGroup::select();
            foreach ($list as $k=>$v){
                $list[$k]['addtime'] = date('Y-m-d H:i:s',$v['addtime']);
            }
            return ['code'=>0,'msg'=>'获取成功！','data'=>$list];
        }
        return view();
    }
    //添加分组
    public function groupAdd(){
        if(request()->isPost()){
            $data=input('post.');
            $result = $this->validate($data, 'AuthGroup');
            if (true !== $result) {
                return ['code'=>1,'msg'=>$result];
            } else {
                $result = AuthGroup::create($data);
                if ($result) {
                    return ['code'=>0,'msg'=>'用户组添加成功'];
                } else {
                    return ['code'=>1,'msg'=>'用户组添加失败'];
                }
            }
        }else{
            return view('form',['info' => '']);
        }
    }
    //修改分组
    public function groupEdit(){
        if(request()->isPost()) {
            $data=input('post.');
            $result = AuthGroup::editSave($data);
            return $result;
        }else{
            $id = input('id');
            $info = AuthGroup::getInfo($id);
            return view('form',['info' =>$info]);
        }
    }
    //删除管理员分组
    public function groupDel(){
        $id = Request::post('id');
        if ($id>1) {
            AuthGroup::destroy($id);
            AuthGroupAccess::where('group_id',$id)->delete();
            return ['code'=>0,'msg'=>'删除成功!'];
        }else{
            return ['code'=>1,'msg'=>'超级管理员组不可删除!'];
        }
    }
    //分组配置规则
    public function groupAccess(){
        $id = input('id');
        $info = AuthGroup::getInfo($id);

        $ruleArr = explode(',',$info['rules']);
        $rule = AuthRule::order('sort asc')->field('id,pid,name,title')->select()->toArray();
        $menus = getTree($rule,0,$ruleArr);
        $view = [
            'info'      => $info,
            'menus' => json_encode($menus)
        ];
        return view('group_access',$view);
    }
    //分配权限
    public function groupSetaccess(){
        $rules = input('post.rules');
        $rules = implode(',',$rules);
        if(empty($rules)){
            return ['msg'=>'请选择权限！','code'=>1];
        }
        $where['id'] = input('post.id');
        if(AuthGroup::update(['rules'=>$rules],$where)){
            return array('msg'=>'权限配置成功！','code'=>0);
        }else{
            return array('msg'=>'保存错误','code'=>1);
        }
    }

    /********************************权限管理*******************************/
    // 权限列表
    public function rule(){
        if(request()->isPost()) {
            $list = AuthRule::order('sort asc')->select();
            $list = tree($list);
            return ['code'=>0,'msg'=>'获取成功！','data'=>$list,'rel'=>1];
        }
        return view();
    }
    //添加权限
    public function add(){
        if(request()->isPost()){
            $data = input('post.');
            $result = $this->validate($data, 'AuthRule');
            if (true !== $result) {
                return ['code'=>1,'msg'=>$result];      // 验证失败 输出错误信息
            }
            $data['addtime'] = time();
            if(AuthRule::create($data)){
                return ['code'=>0,'msg'=>'权限添加成功'];
            }else{
                return ['code'=>1,'msg'=>'权限添加失败'];
            }
        }else{
            $list = AuthRule::order('sort asc')->select();
            $list = tree($list);

            $pid = input('param.id') ? input('param.id') : 0;

            return view('rule_form',['list'=>$list,'info'=>'','pid'=>$pid]);
        }
    }
    //字段值修改
    public function cf(){
        $data = input('post.');
        $result = AuthRule::editSave($data);
        return $result;
    }

    //权限修改
    public function edit(){
        if (request()->isPost()) {
            $data = input('post.');
            $result = AuthRule::editSave($data);
            return $result;
        } else {
            $id = input('param.id');

            $list = AuthRule::order('sort asc')->where('id','<>',$id)->select();
            $list = tree($list);

            $info = AuthRule::find($id);

            return view('rule_form',['list'=>$list,'info'=>$info]);
        }
    }
    //删除权限
    public function del(){
        $id = input('post.id');
        $this->delauth($id);
        cache('AuthRule', NULL);
        cache('authRuleList', NULL);
        return ['code'=>0,'msg'=>'删除成功！'];
    }
    function delauth($id){
        AuthRule::destroy($id);
        $authSon = AuthRule::where('pid',$id)->select();
        if(!empty($authSon)){
            foreach ($authSon as $k=>$v){
                $this->delauth($v['id']);
            }
        }
        return true;
    }
}